<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <script>
        /* let url = 'https://www.suning.com/?source=hao123&medium=mingzhan#abcs'
        let askIndex = url.indexOf('?');
        let wellIndex = url.indexOf('#');
        // 从问号索引位置截取得到 井号 索引位置 不包含井号
        let askText = url.substring(askIndex + 1, wellIndex);
        // 截取井号 后面的
        let wellText = url.substring(wellIndex + 1);
        // 2.问号后面值详细处理
        let askAry = askText.split("&")
        let res = {};
        askAry.forEach((item, index) => {
            // item: 当前数组中循环这一项
            // 以等号分割
            let n = item.split('=')
            // 属性名为 数组的第一项，属性值为第二项
            res[n[0]] = n[1];
        });
        res['HASH'] = wellText
        console.log(res); */

        // 优化封装 取出url参数

        /* function queryUrlParams(url) {
            let askIn = url.indexOf('?'),
                wellIn = url.indexOf('#'),
                askText = '',
                wellText = '';
            wellIn === -1 ? wellIn = url.length : null;
            // 如果问号存在，从问号所引处截取 到井号结束
            askIn > 0 ? askText = url.substring(askIn + 1, wellIn) : null;
            // 井号内容
            wellText = url.substring(wellIn + 1);
            // 2.获取每一部分信息
            let res = {};
            wellText !== "" ? res['HASH'] = wellText : null;
            if (askText !== '') {
                let ary = askText.split('&');
                ary.forEach(item => {
                    let itemAry = item.split('=');
                    res[itemAry[0]] = itemAry[1];
                })
            }
            return res;
        }
        let url = "https://www.suning.com/?source=hao123&medium=mingzhan#abcs"
        let news = queryUrlParams(url)
        console.log(news); */

        // ======================== 正则封装
          function queryUrlParams(url) {
             let res = {},
                 reg1 = /([^?=&#]+)=([^?=&#]+)/g,
                 reg2 = /#([^?=&#]+)/g;
             url.replace(reg1, (n, x, y) => res[x] = y);
             url.replace(reg2, (n, x) => res['HASH'] = x);
             return res;
         }

         let url = "https://www.suning.com/?source=hao123&medium=mingzhan#abcs"
         let news = queryUrlParams(url)
         console.log(news); 
    </script>
</body>

</html>